let requestUrl;

function addHandler() {
    requestUrl = "/chongwu_shizhuang_category/insert";

    $("#yuehuahuanyi_dialog").dialog("open");
}

function editHandler() {
    let rowData = $("#yuehuahuanyi_list").datagrid("getSelected");

    if(rowData) {
        requestUrl = "/chongwu_shizhuang_category/updateById";

        $("#id").val(rowData.id);
        $("#name").textbox("setValue", rowData.name);
        $("#grade").numberspinner("setValue", rowData.grade);
        $("#chongwuId").combobox("setValue", rowData.chongwuId);

        $("#yuehuahuanyi_dialog").dialog("open");
    } else {
        alertMsg("请选择要修改的记录！", "warning");
    }
}

function imageHandler() {
    let rowData = $("#yuehuahuanyi_list").datagrid("getSelected");

    if(rowData) {
        requestUrl = "/chongwu_shizhuang_category/updateImageById";

        $("#shizhuangId").val(rowData.id);
        $("#img").attr("src", rowData.image);

        $("#upload_dialog").dialog("open");
    } else {
        alertMsg("请选择要修改的记录！", "warning");
    }
}

$(document).ready(function() {

    $("#name").textbox({
        width: 120,
        required: true
    });

    $("#grade").numberspinner({
        min: 55,
        max: 115,
        width: 120,
        required: true
    });

    fetchSelect("#chongwuId", "/chongwu_category/selectAll", {
        width: 120,
        editable: true,
        required: true,
        formatter: function(row) {
            return "<img src='" + row.image + "' height='18' />&nbsp;" + row.name;
        },
        onSelect: function (record) {
            $("#grade").numberspinner("setValue", record.grade);
        }
    });

    $("#yuehuahuanyi_dialog").dialog({
        title: "宠物时装",
        width: 500,
        height: 300,
        buttons: [{
            iconCls: "icon-save",
            text: "保存",
            handler: function() {
                let selector = "#yuehuahuanyi_form";

                checkForm(selector, function () {
                    let data = $(selector).serialize();

                    ajaxPost(requestUrl, data, function(resp) {
                        showMsg(resp.message);

                        $(selector).form("clear");
                        $("#yuehuahuanyi_dialog").dialog("close");
                        $("#yuehuahuanyi_list").datagrid("reload");
                    }, error);
                });
            }
        }, {
            iconCls: "icon-cancel",
            text: "取消",
            handler: function() {
                $("#yuehuahuanyi_form").form("clear");
                $("#yuehuahuanyi_dialog").dialog("close");
            }
        }]
    });

    /******************************************************************************************************/

    fetchUploadFilebox("#yuehuahuanyi_list", "/chongwu_shizhuang_category/upload");

    fetchUploadDialog("/chongwu_category/updateImageById", function () {
        $("#yuehuahuanyi_list").datagrid("reload");
    });

    /******************************************************************************************************/

    /**
     * 表格体的高度
     * @type {number}
     */
    let heigt_body = heigt_tr_with_image * 10;
    /**
     * 表格高度
     * @type {number}
     */
    let height_table = heigt_tool + heigt_header + + heigt_filter + heigt_body + heigt_footer + heigt_scoll_bar + 2;

    let datagrid = $("#yuehuahuanyi_list").datagrid({
        url: "/chongwu_shizhuang_category/selectByPage",
        method: "get",
        height: height_table,
        fitColumns: true,
        pagination: true,
        remoteFilter: true,
        clientPaging: false,
        toolbar: [{
            iconCls: "icon-add",
            text: "添加",
            handler: function() {
                addHandler();
            }
        }, "-", {
            iconCls: "icon-edit",
            text: "修改",
            handler: function() {
                editHandler();
            }
        }, "-", imageUpdateButton],
        columns: [[
            {field: "id", title: "编号", align: "center"},
            {field: "name", title: "时装名称", align: "center", width: 50},
            {field: "image", title: "时装图片", align: "center", width: 50
                , formatter: function(value) {
                    return "<img height='" + size + "' src='" + value + "' />";
                }
            },
            {field: "grade", title: "时装等级", align: "center", width: 50
                , formatter: function(value) {
                    return "<div>" + value + "级</div>";
                }
            },
            {field: "chongwuId", title: "宠物类型", align: "center", width: 50
                , formatter: function(value, rowData, rowIndex) {
                    ajaxGet("/chongwu_category/selectById", {
                        id: value
                    }, function(resp) {
                        let data = resp.data;

                        $("#chongwu_" + rowIndex).attr("src", data.image)
                            .attr("title", data.name);
                    }, error);

                    return "<img height='" + size + "' id='chongwu_" + rowIndex + "' />";
                }
            }
        ]]
    });

    datagrid.datagrid("enableFilter", [{
        field: "grade",
        type: "numberspinner",
        options: {
            min: 0,
            max: 115
        },
        operator: ["equal", "notequal", "less", "lessorequal", "greater", "greaterorequal"]
    }, {
        field: "chongwuId",
        type: "combobox",
        options: {
            url: "/chongwu_category/selectAll",
            valueField: "id",
            textField: "name",
            panelHeight: "auto",
            formatter: function(row) {
                return "<img src='" + row.image + "' height='18' />&nbsp;" + row.name;
            }
        },
        operator: ["equal", "notequal"]
    }]);

});